package com.game.modular.game.playerInfo.service;

import cn.hutool.extra.spring.SpringUtil;
import com.game.comment.exection.ServiceException;
import com.game.comment.util.JwtUtils;
import com.game.modular.game.playerAssets.service.PlayerBagServiceImpl;
import com.game.modular.game.playerInfo.domain.Player;
import com.game.modular.game.playerInfo.service.base.PlayerBaseServiceImpl;
import com.game.modular.game.playerMatch.service.PlayerMatchServiceImpl;
import com.game.modular.game.playerMatch.service.PvpRanksServiceImpl;
import com.game.modular.game.playerAssets.service.PlayerSkillServiceImpl;
import com.game.modular.game.playerAssets.service.PlayerHeroServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 游戏账户(Player)表服务实现类
 *
 * @author cxy
 * @since 2024-07-18 10:05:14
 */
@Slf4j
@Service
public class PlayerServiceImpl extends PlayerBaseServiceImpl {
    @Value("${secret.key}")
    private String key;
    @Resource
    private PlayerBagServiceImpl playerBagService;
    @Resource
    private PlayerHeroServiceImpl playerHeroService;
    @Resource
    private PvpRanksServiceImpl pvpRanksService;
    @Resource
    private PlayerSkillServiceImpl playerSkillService;
    @Resource
    private PlayerRankServiceImpl playerRankServiceImpl;
    @Resource
    private PlayerMatchServiceImpl playerMatchServiceImpl;

    private Player init(Long playerId) {
        Player account = Player.builder().uid(playerId).build();
        this.save(account);
        this.pvpRanksService.init(playerId);
        this.playerBagService.registerInit(playerId);
        this.playerHeroService.registerInit(playerId);
        this.playerSkillService.registerInit(playerId);
        this.playerRankServiceImpl.init(playerId);
        return this.selectCache(playerId);
    }

    @PostConstruct
    public void initManMachine() {
        List<Long> longs = playerMapper.selectManMachine();
        Map<String, String> map = new HashMap<>();
        if (longs.size() != 500) {
            for (long i = 1; i < 501; i++) {
                Player init = init(i);
                init.setType(2);
                this.updateById(init);

                String jwtToken = JwtUtils.createJwtToken(init.getUid() + "," + init.getNickname());
                if (jwtToken == null){
                    throw new RuntimeException("生成token失败");
                }
                String key = String.format("player:%d:loginKey", init.getUid());
                ValueOperations<Object, Object> bean = SpringUtil.getBean("valueOperations");
                bean.set(key, jwtToken);
            }
        }
    }

    public List<String> getManMachine() {
        List<Player> playerList = playerMapper.obtainManMachine();
        return playerList.stream().map(item -> JwtUtils.createJwtToken(item.getUid() + "," + item.getNickname())).collect(Collectors.toList());
    }

    @Transactional
    public String login(Long playerId, String serveKey) {
        if (!serveKey.equals(this.key)) {
            throw new ServiceException("非法访问");
        } else {
            String jwtToken = JwtUtils.getToken(playerId);
            if (jwtToken != null) {
                String jwtToken1 = JwtUtils.createJwtToken(playerId + "," + serveKey);

                assert jwtToken1 != null;

                if (jwtToken1.equals(jwtToken)) {
                    return jwtToken;
                }
            }
            Player player = select(playerId);
            String token;
            if (player == null) {
                player = this.init(playerId);
                token = JwtUtils.updateJwtToken(player);
            } else {
                player.setLastLoginTime(new Date());
                this.updateById(player);
                token = JwtUtils.updateJwtToken(player);
            }

            return token;
        }
    }

    @Transactional
    public String createUser(Long playerId, String nickname, Integer sex, String avatar, Integer camp) {
        Player player = this.select(playerId);
        player.setNickname(nickname);
        player.setSex(sex);
        player.setAvatar(avatar);
        player.setCamp(camp);
        this.updateById(player);
        return JwtUtils.updateJwtToken(player);
    }
}
